#!/bin/bash
#
# Copyright (c) 2020 Huawei Technologies Co.,Ltd.
#
# openGauss is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#
#          http://license.coscl.org.cn/MulanPSL2
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# ---------------------------------------------------------------------------------------
#
# snapshot.cfg
#     Configuration for DB4AI.Snapshot functionality.
#
#
# ---------------------------------------------------------------------------------------
export INSTALL_MODEL_WAREHOUSE=false   ###<------------
export INSTALL_SNAPSHOTS=true          ###<------------

#############################################################
### Override user setting for Jenkins / CI
#############################################################
if [[ -n $DB4AI_CLUSTER_CONF ]]; then
    source "$DB4AI_CLUSTER_CONF"
    export DATABASE_NAME=$cnf_madlib_db
    export DATABASE_IP=$cnf_cn_hosts
    export DATABASE_PORT=$cnf_base_port
    export DATABASE_USER=$cnf_db_user
    export DATABASE_PASSWORD=$cnf_pg_password
fi

#############################################################
### Assemble connection string
##############################s###############################
case $DATABASE_COMPATIBILITY in
    a|'')  # the default
        ;;
    b|c)
        export DATABASE_NAME="${DATABASE_NAME}_$DATABASE_COMPATIBILITY"
        ;;
    *)
        echo "Invalid database compatibility mode: $DATABASE_COMPATIBILITY" >&2
        exit -1
        ;;
esac

export GSQL_CMD_FLAGS="$DATABASE_NAME $DATABASE_USER -r"
if [[ ! -z "$DATABASE_PORT" ]]; then
    GSQL_CMD_FLAGS="$GSQL_CMD_FLAGS -p $DATABASE_PORT"
fi
if [[ ! -z "$DATABASE_IP" ]]; then
    GSQL_CMD_FLAGS="$GSQL_CMD_FLAGS -h $DATABASE_IP"
fi
if [[ ! -z "$DATABASE_PASSWORD" ]]; then
    GSQL_CMD_FLAGS="$GSQL_CMD_FLAGS -W $DATABASE_PASSWORD"
fi

#############################################################
### Common utility functions for bash install & test scripts
#############################################################

PAD=$(printf '.%.0s' {1..30})

# empty cleanup hook
CLEANUP() { :; }

RED() { echo "\e[1;31m$@\e[0m"; }
GREEN() { echo "\e[1;32m$@\e[0m"; }
YELLOW() { echo "\e[1;33m$@\e[0m"; }

PASS=$(GREEN 'PASS\n')
SKIP=$(YELLOW 'SKIP\n')
FAIL=$(RED 'FAIL\n')

#overwrite some pre-settings from db4ai.cfg - just in case
err_report() {
    printf $FAIL
    printf "Error on line $(readlink -f ${BASH_SOURCE[1]}):$1\n"
    if [[ $CURRENT_LOG ]]; then
        printf "Detailed output from: $CURRENT_LOG\n"
        grep -i 'fail\|fatal\|error' --color -nC 10 "$CURRENT_LOG" || tail "$CURRENT_LOG"
    fi
    CLEANUP
}

trap 'err_report $LINENO' ERR
